package com.kws.tool.mapper;

import com.kws.tool.dto.ChatDto;
import com.kws.tool.entity.MerchantUserChat;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kws.tool.vo.UserMessageVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

import org.apache.ibatis.annotations.Update;


/**
 * 商家用户聊天记录(MerchantUserChat)表数据库访问层
 *
 * @author yxy
 * @since 2024-10-31 10:45:05
 */

public interface MerchantUserChatMapper extends BaseMapper<MerchantUserChat> {

    @Select("SELECT " +
            " b.id AS merchant_id, " +
            " b.company_name AS merchant_name, " +
            " b.logo AS merchant_img, " +
            " a.`status`, " +
            " a.content, " +
            " a.file, " +
            " a.content_type, " +
            " a.create_time  " +
            "FROM " +
            " tb_merchant_user_chat AS a " +
            " LEFT JOIN tb_merchant AS b ON a.send_user = b.id  " +
            "WHERE " +
            " a.ENABLE = 1  " +
            " AND a.to_user = ${userId}  " +
            " AND a.message_type = ${messageType}  " +
            " ORDER BY " +
            " a.create_time DESC" +
            " limit ${page},${size}")
    List<UserMessageVo> getUserMessage(@Param("userId") Long userId,
                                       @Param("messageType") Integer messageType,
                                       @Param("page") Integer page,
                                       @Param("size") Integer size);

    @Select("SELECT " +
            " count(a.id) " +
            "FROM " +
            " tb_merchant_user_chat AS a " +
            " LEFT JOIN tb_merchant AS b ON a.send_user = b.id  " +
            "WHERE " +
            " a.ENABLE = 1  " +
            " AND a.to_user = ${userId}  " +
            " AND a.message_type = ${messageType}  ")
    Long getUserMessageCount(@Param("userId") Long userId,
                             @Param("messageType") Integer messageType);

    @Select("select send_user,count(send_user) as count from tb_merchant_user_chat  where  to_user=${userId} and type=${type} and `status`=1 GROUP BY send_user; ")
    List<MerchantUserChat> unreadMessage(@Param("userId") Long userId,@Param("type")Integer type);

    /**
     * 查询用户发送方数据
     *
     * @param sendUser
     * @param userId
     * @return
     */
    @Select("select * from tb_merchant_user_chat  where  (to_user=${sendUser} and send_user=${userId}) or (to_user=${userId} and send_user=${sendUser}) and type=${type}  ORDER BY create_time;")
    List<MerchantUserChat> listBySendUser(@Param("sendUser") Long sendUser, @Param("userId") Long userId,@Param("type") Integer type);

    /**
     * 根据发送者id修改当前用户未读信息为已读
     *
     * @param sendUser
     * @param userId
     * @return
     */
    @Update("update tb_merchant_user_chat set status=0 where send_user=${sendUser} and to_user=${userId}")
    boolean readMessage(@Param("sendUser") Long sendUser, @Param("userId") Long userId);

    @Select("SELECT " +
            " b.id AS merchant_id, " +
            " b.company_name AS merchant_name, " +
            " b.logo AS merchant_img, " +
            " a.`status`, " +
            " a.content, " +
            " a.file, " +
            " a.content_type, " +
            " a.create_time  " +
            "FROM " +
            " tb_merchant_user_chat AS a " +
            " LEFT JOIN tb_merchant AS b ON a.send_user = b.id  " +
            "WHERE " +
            " a.ENABLE = 1  " +
            " AND a.to_user = ${userId}  " +
            " AND a.message_type = 3  " +
            " AND a.create_time = ( " +
            " SELECT " +
            "  MAX( a1.create_time )  " +
            " FROM " +
            "  tb_merchant_user_chat AS a1  " +
            " WHERE " +
            "  a1.send_user = b.id  " +
            "  AND a1.ENABLE = 1  " +
            "  AND a1.to_user = ${userId}  " +
            "  AND a1.message_type = ${messageType}  " +
            " )" +
            "ORDER BY " +
            "    create_time DESC" +
            " limit ${page},${size} ")
    List<UserMessageVo> getServeMessage(@Param("userId") Long userId,
                                        @Param("messageType") Integer messageType,
                                        @Param("page") Integer page,
                                        @Param("size") Integer size);

    @Select("SELECT " +
            " count(b.id) " +
            "FROM " +
            " tb_merchant_user_chat AS a " +
            " LEFT JOIN tb_merchant AS b ON a.send_user = b.id  " +
            "WHERE " +
            " a.ENABLE = 1  " +
            " AND a.to_user = ${userId}  " +
            " AND a.message_type = 3  " +
            " AND a.create_time = ( " +
            " SELECT " +
            "  MAX( a1.create_time )  " +
            " FROM " +
            "  tb_merchant_user_chat AS a1  " +
            " WHERE " +
            "  a1.send_user = b.id  " +
            "  AND a1.ENABLE = 1  " +
            "  AND a1.to_user = ${userId}  " +
            "  AND a1.message_type = ${messageType}  " +
            " )")
    Long getServeMessageCount(@Param("userId") Long userId,
                              @Param("messageType") Integer messageType);
}
